package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.example.system.entity.Post;
import org.example.system.entity.Remark;
import org.example.system.entity.User;
import org.example.system.mapper.RemarkMapper;
import org.example.system.service.RemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

/**
 * 评论服务层
 */
@Service
public class RemarkServiceImpl implements RemarkService {
    @Autowired
    private RemarkMapper remarkMapper;

    /**
     * 按条件分页查询
     * 影响条件有评论标题
     * @param remark
     * @return
     */
    @Override
    public IPage<Remark> selectPage(Remark remark){
        LambdaQueryWrapper<Remark> wrapper = new LambdaQueryWrapper<>();
        IPage<Remark> page = new Page<>();
        page.setCurrent(remark.getPageNum());
        page.setSize(remark.getPageSize());
        return remarkMapper.selectPage(page,wrapper);
    }

    /**
     * 按分页查询联查用户表
     * @param remark
     * @return
     */
    @Override
    public IPage<Remark> select(Remark remark){
        MPJLambdaWrapper<Remark> wrapper = new MPJLambdaWrapper<>();
        wrapper.eq(Remark::getPostId,remark.getPostId());
        wrapper.selectAll(Remark.class);
        wrapper.leftJoin(User.class, User::getId, Remark::getUserId);
        wrapper.selectAs(User::getNickname,Remark::getNickname);
        wrapper.selectAs(User::getAvatarPath,Remark::getAvatarPath);
        wrapper.orderByDesc(Remark::getId);
        IPage<Remark> page = new Page<>();
        page.setCurrent(remark.getPageNum());
        page.setSize(remark.getPageSize());
        return remarkMapper.selectPage(page,wrapper);
    }

    /**
     * 添加评论
     * @param remark
     * @return
     */
    @Override
    public int add(Remark remark) {
        remark.setUpdateTime(new Date());
        remark.setNote("评论创建成功！");
        return remarkMapper.insert(remark);
    }

    /**
     * 修改
     * @param remark
     * @return
     */
    @Override
    public int update(Remark remark) {
        remark.setUpdateTime(new Date());
        remark.setNote("修改评论数据");
        return remarkMapper.updateById(remark);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(Long[] ids) {
        return remarkMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据id获取信息
     * @param id
     * @return
     */
    @Override
    public Remark getOne(Long id) {
        return remarkMapper.selectById(id);
    }
}
